package com.dayaokeji.rhythmschoolstudent.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Gorden on 2017/10/13.
 * 生成座位数据工具类
 */

public class SeatUtils {

    private static int mSeatRowNum;
    private static int mSeatColumnNum;

    /**
     * 将座位排布字符串拆装成二维字符数组
     *
     * @param seat
     * @return
     */
    private static char[][] splitSeat(String seat) {

        String[] seatRows = seat.split("\r\n");
        mSeatRowNum = seatRows.length;
        mSeatColumnNum = seatRows[0].length();
        if (mSeatRowNum > 0 && mSeatColumnNum > 0) {
            char[][] seatArray = new char[mSeatRowNum][mSeatColumnNum];

            for (int i = 0; i < seatRows.length; i++) {
                String seatRow = seatRows[i];
                char[] chars = seatRow.toCharArray();
                for (int j = 0; j < chars.length; j++) {
                    seatArray[i][j] = chars[j];
                }
            }
            return seatArray;
        }
        return null;
    }

    /**
     * 根据座位排布，给用户按照顺序进行排座
     * @return
     */
    public static Map<Integer, String> generateUsersMap(String seat, List<Integer> list) {
        Map<Integer,String> usersMap = new HashMap<>();
        char[][] splitSeat = splitSeat(seat);
        int index = 0;
        for (int i = 0; i < splitSeat.length; i++) {
            char[] chars = splitSeat[i];
            for (int j = 0; j < chars.length; j++) {

                char aChar = chars[j];
                if (aChar == '@') {
                    if (usersMap.size() < list.size()) {
                        Integer userId = list.get(index);
                        usersMap.put(userId,(i+1)+"排"+(j+1)+"座");
                        index ++;
                    }
                }
            }
        }
        return usersMap;
    }

    /**
     * 生成用户座位号
     * @return
     */
    public static List<Map<String,String>> getUserSeatList(String seat,List<Integer> list) {
        List<Map<String,String>> lists = new ArrayList<>();
        Map<Integer, String> usersMap = generateUsersMap(seat,list);
        Set<Map.Entry<Integer, String>> entries = usersMap.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            Map<String,String> map = new HashMap<>();
            map.put("userId",String.valueOf(entry.getKey()));
            map.put("seat",entry.getValue());
            lists.add(map);
        }

        return lists;
    }

}
